﻿namespace Cms.Domain
{
    public class DashboardGadget
    {
        private SourceCode _code;
        private string _filename;
        private SourceCode _xml;

        public virtual int ID { get; set; }

        public virtual string Filename
        {
            get { return _filename; }
            set { _filename = value; }
        }

        public virtual bool Activated { get; set; }

        public virtual bool CustomGadget { get; set; }
        
        public virtual SourceCode Code
        {
            get { return _code; }
            set { _code = value; }
        }
        
        public virtual SourceCode Xml
        {
            get { return _xml; }
            set { _xml = value; }
        }
        
        public virtual string GetCode()
        {
            return _code != null ? _code.CurrentVersion.Code : string.Empty;
        }

        public virtual void SetCode(string code, Administrator editedBy)
        {
            if (_code == null)
                _code = new SourceCode();
            _code.AddVersion(code, editedBy);
        }
        
        public virtual string GetXml()
        {
            return _xml != null ? _xml.CurrentVersion.Code : string.Empty;
        }
        
        public virtual void SetXml(string code, Administrator editedBy)
        {
            if (_xml == null)
                _xml = new SourceCode();
            _xml.AddVersion(code, editedBy);
        }
        
        public virtual string GetFilenameWithoutExtension()
        {
            return _filename.Substring(0, _filename.Length - 5);
        }
    }
}